// httpRequestTool
import "babel-polyfill";
import {Promise, Polyfill} from 'es6-promise-polyfill';
import axios from 'axios'

// 调试设置
if (window.location.hostname === 'localhost' && window.location.port === '8099') {
  axios.defaults.baseURL = 'http://localhost:8080';
}

axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest';
axios.defaults.timeout = 30000;
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Do something with response data
  return response;
}, function (error) {
  // Do something with response error
  // return Promise.reject(error);
  if (error.response) {
    // The request was made and the server responded with a status code
    // that falls out of the range of 2xx
    console.log(error.response.data);
    console.log(error.response.status);
    // console.log(error.response.headers);
    // console.log(error);
    alert(error.response.data.message);
  } else if (error.request) {
    // The request was made but no response was received
    // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
    // http.ClientRequest in node.js
    console.log(error.request);
    alert(error + "; " + error.request);
  } else {
    // Something happened in setting up the request that triggered an Error
    console.log('Error', error.message);
    alert('Error', error.message);
  }
  console.log(error.config);
  return Promise.reject(error);
});

export default axios;
